<?php
// 20090211
// GetLangString

 
	if ($_REQUEST['add']) {
		$addfriend	=	escapestr($_REQUEST['add']);	
		if ($addfriend == $id) {
			$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_friendrequest"),GetLangString($lang,"msg_friendaddself"));		
		}
		$query      = "SELECT * from friends where user1='$id' and user2='$addfriend'";
		$result = mysql_query($query);
		if ($res = mysql_num_rows($result)){ 
			$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_err"),GetLangString($lang,"msg_errisfriend"));
		} else {
			$query      = "INSERT into friends (`user1`,`user2`) values ('$id','$addfriend')";
			$result = mysql_query($query);
			if ($result) {
				$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_friendrequest"),GetLangString($lang,"msg_friendrequest"));
			} else {
				$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_err"),GetLangString($lang,"msg_dberror"));
			}
		}	 
	} else 


	if ($_REQUEST['delete']) {
		$deletefriend	=	escapestr($_REQUEST['delete']);	
		$query  = "DELETE from friends where (`user1`='$id' and `user2`='$deletefriend') or (`user2`='$id' and `user1`='$deletefriend')";		
		$result = mysql_query($query);
		if ($result) {
			$SITE_MIDDLE .= FormatELement(GetLangString ($lang,"txt_delete"),GetLangString ($lang,"msg_frienddeleted")); 		
		} else {
			$SITE_MIDDLE .= FormatELement(GetLangString ($lang,"txt_error"),GetLangString ($lang,"msg_dberror"));		
		}		
	
	}


	$query        = "SELECT user2 from friends where user1='$id'";
	$result = mysql_query($query);
	if ($res = mysql_num_rows($result)){ 
		while ($res > $i){
			$friendid = mysql_result($result,$i++);
			$query2        = "SELECT * from users where id='$friendid'";
			$result2 		= mysql_query($query2);
			$profileinfo 	= mysql_fetch_array($result2);

			$query3       = "SELECT * from friends where user2='$id' and user1='$friendid'";
			$isfriend 	= mysql_query($query3);
			$status = GetStatusImg($profileinfo['status']);
			$img = "/index.php?do=getphoto&id=".$profileinfo['photo']."&size=75&square=1";			
			
			//$img = "images/sheep.png";
			if (mysql_num_rows($isfriend)){ 
				$friends .= " <div class=\"userthumb\">
						<a href=\"/index.php?do=profile&profile_id=".$profileinfo['id']."\">						
						".$profileinfo['name']."</a><br>$status<br><img src=\"$img\" alt=\"$TXT_NOIMG\" 
							width=\"75\" height=\"75\"><br><a href=\"/index.php?do=friends&delete=$friendid\">".GetLangString($lang, "txt_delete")."</a></div>";
			} else { 
				$req .=  " <div class=\"userthumb\">
						<a href=\"/index.php?do=profile&profile_id=".$profileinfo['id']."\">						
						".$profileinfo['name']."</a><br>$status<br><img src=\"$img\" alt=\"$TXT_NOIMG\" 
							width=\"75\" height=\"75\"><br><a href=\"/index.php?do=friends&delete=$friendid\">".GetLangString($lang, "txt_delete")."</a></div>";			}	
		}
	}


	$query4        = "SELECT user1 from friends where user2='$id'";
	$result4 = mysql_query($query4);
	if ($res = mysql_num_rows($result4)){ 
		$i=0;
		while ($res > $i){
			$friendid = mysql_result($result4,$i++);
			$query5       = "SELECT * from friends where user1='$id' and user2='$friendid'";
			$isfriend 	= mysql_query($query5);
			if (!mysql_num_rows($isfriend)) {
				$query6        = "SELECT * from users where id='$friendid'";
				$result6 		= mysql_query($query6);
				$profileinfo 	= mysql_fetch_array($result6);
				$img = "/index.php?do=getphoto&id=".$profileinfo['photo']."&size=75&square=1";			
				$wanabe .=  " <div class=\"userthumb\">
						<a href=\"/index.php?do=profile&profile_id=".$profileinfo['id']."\">						
						".$profileinfo['name']."</a><br>$status<br><img src=\"$img\" alt=\"$TXT_NOIMG\" 
							width=\"75\" height=\"75\"><br><a href=\"/index.php?do=friends&delete=$friendid\">".GetLangString($lang, "txt_delete")."</a></div>";
			}
		}
	}

	$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_myfriends"),GetLangString($lang,"msg_myfriends")."<br>$friends");
	$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_sentreq"),GetLangString($lang,"msg_sentreq")."<br>$req");
	$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_recvreq"),GetLangString($lang,"msg_recvreq")."<br>$wanabe");


?>
